package com.hd.trans.share;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.hd.trans.R;
import com.hd.trans.utils.ShareUtil;

import java.io.File;

public class SharePdfModel {
    private final String TAG = "SharePdfModel";

    private ProgressDialogD mProgressDialog = null;

    /**
     * 分享txt文件
     * @param content  保存的文本内容
     * @param file     保存到哪个文件
     */
    public void shareTxt(Activity activity, String content, File file) {
        showProgress(activity, EnShareFileType.TXT_FILE);
        boolean success = FileRWExtUtil.saveText(content, file.getAbsolutePath());
        if (success) {
            ShareUtil.shareFile(activity, file, "");
        } else {
            Log.d("ShareManagerModel", "分享失败--------");
            Toast.makeText(activity, "分享失败", Toast.LENGTH_SHORT).show();
        }
        safeDismiss();
    }

    /**
     * 文本内容 直接请求接口转word/pdf 分享
     *
     * @param activity
     * @param content  文本内容
     * @param fileType 类型
     * @param saveName 保存的名字
     */
    public void requestShareWord(Activity activity, String content, String language, int fileType, String saveName) {
        showProgress(activity, fileType);

        TextToPdfParam params = new TextToPdfParam(content, saveName, language);
        params.setFileType(fileType);

        // request
        new TextToPdfModel().requestTextToPdf(params, saveName, new TextToPdfModel.TextToPdfModelCallback() {
            @Override
            public void onTextToPdfModelStart() {
                Log.d(TAG, "onTextToPdfModelStart------------>");
                showProgress(activity, fileType);
            }

            @Override
            public void onTextToPdfModelProgress(int progress) {
                Log.d(TAG, "onTextToPdfModelProgress-----------progress->" + progress);
                if (mProgressDialog != null) {
                    mProgressDialog.setProgress(progress);
                }
            }

            @Override
            public void onTextToPdfModelError(String msg) {
                Log.d(TAG, "onTextToPdfModelError-----------msg->" + msg);
                Toast.makeText(activity, activity.getString(R.string.share_file_fail), Toast.LENGTH_SHORT).show();
                safeDismiss();
            }

           /* @Override
            public void onTextToPdfModelComplete(DownloadArg downloadArg) {

            }*/

            @Override
            public void onTextToPdfModelComplete(DownloadArg downloadArg) {
                Log.d(TAG, "onTextToPdfModelComplete-----------downloadArg->" + downloadArg);
                ShareUtil.shareFile(activity, new File(downloadArg.getTargetFileAbsPaths().get(0)), activity.getString(R.string.share_file));
                safeDismiss();
            }
        });
    }

    private void safeDismiss() {
        if (this.mProgressDialog == null) {
            return;
        }
        if (this.mProgressDialog.isShowing()) {
            try {
                this.mProgressDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void showProgress(Activity context, int fileType) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialogD(context);
        }
        mProgressDialog.setAutoInCrease(true);
        Log.w("拿到的type", fileType + "---------");
        switch (fileType) {
            case EnShareFileType.WORD_FILE: // 正在生成word
                mProgressDialog.setContent(R.string.word_file_creating);
                break;
            case EnShareFileType.EXCEL_FILE: // 正在生成excel
                mProgressDialog.setContent(R.string.xls_file_creating);
                break;
            case EnShareFileType.PDF_FILE: // 正在生成pdf
                mProgressDialog.setContent(R.string.pdf_file_creating);
                break;
            case EnShareFileType.TXT_FILE: //正在生成txt
                mProgressDialog.setContent(R.string.txt_file_creating);
                break;

        }
        mProgressDialog.setMax(99);
        mProgressDialog.setProgress(0);
        mProgressDialog.show();
    }
}
